/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * publicarAviso.java
 *
 * Created on Jul 27, 2011, 1:51:18 AM
 */
package appescritorio;

import Conexion.AccesoADatos;
import Entidades.Aviso;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author martin
 */
public class publicarAviso extends javax.swing.JFrame {

    /** Creates new form publicarAviso */
    public publicarAviso() {
        initComponents();
        AccesoADatos aad = new AccesoADatos();
        ArrayList ar = aad.cargarCombo("select * from puestotrabajo", "nombrePuestoTrabajo");
        for (int i = 0; i < ar.size(); i++) {
            comboPuesto.addItem(ar.get(i));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comboPuesto = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        txtCaducidad = new com.toedter.calendar.JDateChooser();
        txtInicio = new com.toedter.calendar.JDateChooser();
        txtVacantes = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Publicar Aviso");
        setLocationByPlatform(true);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 550, 194));
        setMinimumSize(new java.awt.Dimension(550, 194));

        jLabel1.setText("Puesto de trabajo:");

        jButton1.setText("Publicar nuevo aviso");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Fecha inicio vacante:");

        jLabel3.setText("Vacantes:");

        jLabel4.setText("Fecha caducidad:");

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        txtVacantes.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(comboPuesto, javax.swing.GroupLayout.Alignment.TRAILING, 0, 406, Short.MAX_VALUE)
                    .addComponent(txtCaducidad, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addComponent(txtInicio, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addComponent(txtVacantes, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboPuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtVacantes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtCaducidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (validar()) {
            AccesoADatos aad = new AccesoADatos();
            String area;
            String nombre = comboPuesto.getSelectedItem().toString();
            int diaIn = txtInicio.getDate().getDate();
            int mesIn = txtInicio.getDate().getMonth();
            int anioIn = txtInicio.getDate().getYear();
            String fechaIn = anioIn + "/" + mesIn + "/" + diaIn;
            if (fechaIn.charAt(0) == '1') {
                fechaIn = fechaIn.replaceFirst("1", "20");
            }
            int diaFin = txtCaducidad.getDate().getDate();
            int mesFin = txtCaducidad.getDate().getMonth();
            int anioFin = txtCaducidad.getDate().getYear();
            String fechaFin = anioFin + "/" + mesFin + "/" + diaFin;
            if (fechaFin.charAt(0) == '1') {
                fechaFin = fechaFin.replaceFirst("1", "20");
            }
            int vacantes = (Integer) txtVacantes.getValue();

            if (JOptionPane.showConfirmDialog(null, "Usted está por publicar un aviso para el puesto " + nombre + ", con " + vacantes + " vacantes, fecha de inicio " + fechaIn + " y fecha de caducidad " + fechaFin + ". \n¿Desea confirmar la publicación?", "Confirmacion de los datos", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                ResultSet rs = aad.leer("select area from puestotrabajo where nombrePuestoTrabajo='" + nombre + "'");
                try {
                    rs.next();
                    area = rs.getString("area");
                    Aviso av = new Aviso(fechaIn, area, vacantes, fechaFin, nombre);
                    if (av.insertar()) {
                        JOptionPane.showMessageDialog(this, "El aviso ha sido publicado exitosamente");
                    } else {
                        JOptionPane.showMessageDialog(this, "El aviso no pudo ser publicado");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(publicarAviso.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Por favor complete todos los campos");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private boolean validar() {
        if (this.txtCaducidad.getDate() == null) {
            return false;
        } else if (this.txtInicio.getDate() == null) {
            return false;
        } else if ((Integer) txtVacantes.getValue() == 0) {
            return false;
        }
        return true;
    }

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    Inicio i = new Inicio();
    i.setVisible(true);
    this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    // Set System L&F
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (UnsupportedLookAndFeelException e) {
                    // handle exception
                } catch (ClassNotFoundException e) {
                    // handle exception
                } catch (InstantiationException e) {
                    // handle exception
                } catch (IllegalAccessException e) {
                    // handle exception
                }
                new publicarAviso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboPuesto;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private com.toedter.calendar.JDateChooser txtCaducidad;
    private com.toedter.calendar.JDateChooser txtInicio;
    private javax.swing.JSpinner txtVacantes;
    // End of variables declaration//GEN-END:variables
}
